package com.huirui.server.interceptor;

import com.huirui.server.context.UserContext;
import com.huirui.server.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
public class MiniProgramUserInfoInterceptor implements HandlerInterceptor {
    @Resource
    private JwtUtil jwtUtil;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (request.getHeader("token") != null) {
            Long id = jwtUtil.parseToken(request.getHeader("token"));
            UserContext.setId(id);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        UserContext.removeId();
    }
}
